package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.common.utils.BuildTree;
import com.aisile.crowd_funding.common.vo.Tree;
import com.aisile.crowd_funding.system.mapper.SysMenuMapper;
import com.aisile.crowd_funding.system.mapper.SysRoleMenuMapper;
import com.aisile.crowd_funding.system.pojo.SysMenu;
import com.aisile.crowd_funding.system.pojo.SysMenuExample;
import com.aisile.crowd_funding.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @标题 MenuServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/6 14:05
 * @版本号 1.0
 **/
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return sysMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysMenu record) {
        record.setGmtCreate(new Date());
        record.setGmtModified(new Date());
        return sysMenuMapper.insert(record);
    }

    @Override
    public SysMenu selectByPrimaryKey(Long id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(SysMenu sysMenu) {
        return sysMenuMapper.updateByPrimaryKey(sysMenu);
    }

    @Override
    public List<SysMenu> selectByExample(SysMenuExample sysMenuExample) {
        return sysMenuMapper.selectByExample(sysMenuExample);
    }

    @Override
    public int selectChildCount(Long parentId) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        sysMenuExample.createCriteria().andParentIdEqualTo(parentId);
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(sysMenuExample);
        return sysMenus.size();
    }

    /**
     * 生成  菜单树的方法
     * @return  tree
     */
    @Override
    public Tree getTree() {
        // 查询出所有的  sysMenu数据
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(null);
        // 转换成 tree
        List<Tree> treeList = new ArrayList<>();
        for (SysMenu menu : sysMenus){
            Tree tree = new Tree();
            tree.setId(menu.getMenuId()==null?"0":menu.getMenuId().toString());
            tree.setText(menu.getName());
            tree.setParentId(menu.getParentId().toString());

            treeList.add(tree);
        }
        return BuildTree.buildTree(treeList);
    }

    @Override
    public Tree getTree(Long roleId) {
        // 查询出所有的  sysMenu数据
        List<SysMenu> sysMenus = sysMenuMapper.selectByExample(null);
        // 查询 该角色 所对应的 menuIds
        List<Long> menuIds = sysRoleMenuMapper.selectMenuIdsByRoleId(roleId);
        // 转换成 tree
        List<Tree> treeList = new ArrayList<>();
        for (SysMenu menu : sysMenus){
            Tree tree = new Tree();
            tree.setId(menu.getMenuId()==null?"0":menu.getMenuId().toString());
            tree.setText(menu.getName());
            tree.setParentId(menu.getParentId().toString());
            Map<String, Object> state = new HashMap<>();
            if(menuIds.contains(menu.getMenuId())){
                //选中
                state.put("selected",true);
            }else {
                //未选中
                state.put("selected",false);
            }
            tree.setState(state);
            treeList.add(tree);
        }
        return BuildTree.buildTree(treeList);
    }

    @Override
    public List<SysMenu> selectMenuListByUserId(Long userId) {

        return sysMenuMapper.selectMenuListByUserId(userId);
    }

    @Override
    public Set<String> selectPermsByUserId(Long userId) {
        return sysMenuMapper.selectPermsByUserId(userId);
    }

    @Override
    public Set<String> selectAllPerms() {
        return sysMenuMapper.selectAllPerms();
    }


}
